define('wangwu/sxyc/Channel',
[
	'dojo/_base/declare',
	'dojo/_base/lang',
	'dojo/on',

	'esri/Evented',
	'esri/layers/GraphicsLayer',
	'esri/graphic',
	'esri/geometry/Polygon',
	'esri/symbols/SimpleLineSymbol',
	'esri/symbols/SimpleMarkerSymbol',
	'esri/symbols/PictureMarkerSymbol',
	'esri/Color',
	'esri/geometry/Point',
	'esri/geometry/Polyline',


	'esri/tasks/QueryTask',
	'esri/tasks/query'

],
function(declare, lang, on,
		 Evented, GraphicsLayer, Graphic, Polygon, SimpleLineSymbol,SimpleMarkerSymbol, PictureMarkerSymbol,Color,Point,Polyline,
		 QueryTask, Query
	){
	var d = declare([Evented], {
		_eventMap:{
			'graphic-click':['evt'],
			'layer-add':!0,
			'mouse-over':['graphic','evt'],
			'mouse-out':['graphic','evt']
		},
		constructor:function(map, opt){
			this.map = map;

			this.option = {
				layerName:'主干渠',
				queryUrl:'',
				visibility : true
			};

			lang.mixin(this.option, opt);

			this.layer = new GraphicsLayer({
				id:'Channel'+parseInt(Math.random()*1000)
			});
			this.layer.name = this.option.layerName;
			this.layer.visible = this.option.visibility;

			this.map.addLayer(this.layer);
			this.initialized = false;
			var mouseOver = on(this.layer, 'mouse-over', lang.hitch(this, this._mouseOver));
			var mouseOut = on(this.layer, 'mouse-out', lang.hitch(this, this._mouseOut));
		},
		//---------------------------------------------------------public-----------------------------------------
		init:function(){
			if (this.initialized) return;

			on(this.layer, 'click', lang.hitch(this, this._graphicClick));
			this._init();

			this.initialized = true;
		},
		setFieldRender:function(render){
            this.fieldRender =  render;
        },
		setVisible:function(stats){
			this.visibility =  stats;
		},
		hide:function() {
			this._hide();
		},
		show:function(){
			this._show();
		},
		//----------------------------------------------------------private----------------------------------------
		_init:function(){
			this.queryTask = new QueryTask(this.option.queryUrl);
			this.query = new Query();
			this.query.where = '1=1';
			this.query.outFields = ['*'];
			this.query.returnGeometry = true;
			this.queryTask.execute(this.query, lang.hitch(this, this._queryResult), lang.hitch(this, this._queryError));
		},
		_queryResult:function(e){
			//console.log(e);
			for (var i=0; i < e.features.length; i++){
				var f = e.features[i];
				var symbol,url;
				switch(this.layer.name){
					case "干渠":
						symbol = new SimpleLineSymbol('solid',new Color([44,18,135,1]),5);
						break;
					case "支渠":
						symbol = new SimpleLineSymbol('solid',new Color([70,77,236,0.8]),3);
						break;
					case "断面":
						symbol = new SimpleLineSymbol('solid',new Color([44,18,135,1]),5);
						break;
					case "桩点":
						symbol = new SimpleMarkerSymbol("circle",6, null, new Color([33,255,50,0.85]));
						break;
					case "边界":
						symbol = new SimpleLineSymbol('solid',new Color([233,192,71,0.7]),5);
						break;
					case "泵站":
						if (this.fieldRender){
							url = this.fieldRender;
							symbol = new PictureMarkerSymbol(url, 18, 18);
						}else{
							symbol = new SimpleMarkerSymbol("circle",15, null, new Color([33,255,50,0.85]));
						}
						break;
					case "出水池":
						if (this.fieldRender){
							url = this.fieldRender;
							symbol = new PictureMarkerSymbol(url, 18, 18);
						}else{
							symbol = new SimpleMarkerSymbol("circle",15, null, new Color([33,255,50,0.85]));
						}
						break;
					case "分水闸":
						if (this.fieldRender){
							url = this.fieldRender;
							symbol = new PictureMarkerSymbol(url, 18, 18);
						}else{
							symbol = new SimpleMarkerSymbol("circle",15, null, new Color([33,255,50,0.85]));
						}
						break;
				}
				f.setSymbol(symbol);
				this.layer.add(f);
			}
			(this.layer.visible)?this.show():this.hide();
			this.onLayerAdd();
		},
		_queryError:function(e){
			console.log(e);
		},
		_hide:function(){
			this.layer.hide();
		},
		_show:function(){
			this.layer.show();
		},
		//--------------------------------pre event---------------------------------
		_graphicClick:function(evt){
			this.onGraphicClick(evt);
		},
		_mouseOver:function(e){
			if(e.graphic.symbol){
				switch(e.graphic.symbol.declaredClass){
					case "esri.symbol.SimpleLineSymbol":
						e.graphic.symbol.setWidth(e.graphic.symbol.width*1.5);
						break;
					case "esri.symbol.PictureMarkerSymbol":
					    e.graphic.symbol.setWidth(e.graphic.symbol.width*1.5);
					    e.graphic.symbol.setHeight(e.graphic.symbol.height*1.5);
						break;
					case "esri.symbol.SimpleMarkerSymbol":
						e.graphic.symbol.setSize(e.graphic.symbol.size*1.5);
						break;
				}
			}
			this.layer.redraw();
			this.onMouseOver();
		},
		_mouseOut:function(e){
			if(e.graphic.symbol){
				switch(e.graphic.symbol.declaredClass){
					case "esri.symbol.SimpleLineSymbol":
						e.graphic.symbol.setWidth(e.graphic.symbol.width/1.5);
						break;
					case "esri.symbol.PictureMarkerSymbol":
					    e.graphic.symbol.setWidth(e.graphic.symbol.width/1.5);
					    e.graphic.symbol.setHeight(e.graphic.symbol.height/1.5);
						break;
					case "esri.symbol.SimpleMarkerSymbol":
						e.graphic.symbol.setSize(e.graphic.symbol.size/1.5);
						break;
				}
			}
			this.layer.redraw();
			this.onMouseOut();
		},
		//--------------------------------event---------------------------------------
		onGraphicClick:function(evt){

		},
		onLayerAdd:function(evt){

		},
		onMouseOver:function(){

		},
		onMouseOut:function(){

		}
	});

	return d;
})